﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Framework.Common;
using DomPrint.Common;
using Framework;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace DomPrint.UControl
{
    public partial class UCSetting : UserControl
    {
        /// <summary>
        /// 调用win api将指定名称的打印机设置为默认打印机
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        [DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(String Name);

        #region 属性
        /// <summary>
        /// 
        /// </summary>
        public MediumType TypeName { get; set; }

        #endregion

        public UCSetting()
        {
            InitializeComponent();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (!txtPrintNum.Text.IsInt())
                {
                    DomCommon.ShowMsg("打印数量必须输入数字");
                    txtPrintNum.Focus();
                    txtPrintNum.SelectAll();
                    return;
                }
                bool printerIsReady = false;
                string printerName = DomCommon.GetConfigValue("PrinterName").ToLower();
                string[] names = printerName.Split(';');
                if (!names.Contains(printDialog1.PrinterSettings.PrinterName.ToLower()))
                {
                    foreach (var item in PrinterSettings.InstalledPrinters)
                    {
                        if (names.Contains(item.ToString().ToLower()))
                        {
                            SetDefaultPrinter(item.ToString());
                            printerIsReady = true;
                            break;
                        }
                    }
                    if (!printerIsReady)
                    {
                        DomCommon.ShowMsg("您还没有添加条码打印机,请添加条码打印机");
                        return;
                    }
                }

                //得到打印机的纸张长度和宽度的设置值
                //判断当前打印机中的纸张设置值
                int height = printDialog1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
                int width = printDialog1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
                //判断磁带选择的是那一种类型,从而设置纸张的大小和方向
                if (TypeName == MediumType.Tape)
                {
                    MainForm mf = this.ParentForm as MainForm;
                    if (mf.TapeType == TapeType.Tape9840)
                    {
                        if (!printDialog1.PrinterSettings.DefaultPageSettings.Landscape)
                        {
                            DomCommon.ShowMsg("请在页面设置中调整方向为横向");
                            return;
                        }
                    }
                    else
                    {
                        if (printDialog1.PrinterSettings.DefaultPageSettings.Landscape)
                        {
                            DomCommon.ShowMsg("请在页面设置中调整方向为纵向");
                            return;
                        }
                    }
                }
                else
                {
                    if (printDialog1.PrinterSettings.DefaultPageSettings.Landscape)
                    {
                        DomCommon.ShowMsg("请在页面设置中调整方向为纵向");
                        return;
                    }
                }
                //TODO:页面标签验证大小
                if (height > 2268 || width > 2871)
                {
                    DomCommon.ShowMsg("请检查打印机中标签大小的设置,应设为50mm x 40mm");
                    return;
                }

                //打印条码
                Print();
            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("Print func exception:{0}{1}", ex.Message, ex.StackTrace);
                DomCommon.ShowError("打印出现异常");
            }
        }

        private void btnPrintSet_Click(object sender, EventArgs e)
        {
            printDialog1.ShowDialog();
        }

        private void btnPrintHST_Click(object sender, EventArgs e)
        {
            //TODO:打印航片筒标签
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }

        /// <summary>
        /// 打印
        /// </summary>
        private void Print()
        {
            int printNum = txtPrintNum.Text.ToInt();
            MainForm mf = this.ParentForm as MainForm;
            if (mf.ListItem.Count == 0)
            {
                DomCommon.ShowMsg("请添加编号到列表框");
                return;
            }
            switch ((int)TypeName)
            {
                case (int)MediumType.HD:
                    if (mf.HdType == HDType.HS)
                        new HDHSShowForm(printNum, mf.ListItem).ShowDialog();
                    else
                        new HDDomShowForm(printNum, mf.ListItem, mf.MediumType).ShowDialog();
                    break;
                case (int)MediumType.Tape:
                    if (mf.TapeType == TapeType.SLR)
                        new HDDomShowForm(printNum, mf.ListItem, mf.MediumType, mf.TapeType).ShowDialog();
                    else
                        new Tape9840ShowForm(printNum, mf.ListItem).ShowDialog();
                    break;
                case (int)MediumType.CD:
                case (int)MediumType.DVD:
                case (int)MediumType.Other:
                    new HDDomShowForm(printNum, mf.ListItem, mf.MediumType).ShowDialog();
                    break;
            }
        }
    }
}
